﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
/*
 * 
 *   基本信息的处理
 * 
 */
namespace TRPOP.CMS.Template.GenerateCS
{
    /// <summary>
    /// 将cms标签替换为csharp语言
    /// </summary>
    public partial class ConvertTags
    {
        /*
         * 
         *  解析loop  
         *   模式： 一行一行的解析
         *<loop name="List" var="var" key="key">   == >   
            int intb1d0c18bf2804fcd9091b636098d3e34 = 0 + 1 - 1;
            foreach (DataRow oTempRowsb1d0c18bf2804fcd9091b636098d3e34 in getDT("List").Rows)
            {
                //记录并保存当前位置
                intb1d0c18bf2804fcd9091b636098d3e34++;
                SaveVar("key", intb1d0c18bf2804fcd9091b636098d3e34);

                //保存行变量
                SaveRow("var", oTempRowsb1d0c18bf2804fcd9091b636098d3e34);
         *</loop>   ===>   
         *  }
         * 
         */

        /// <summary>
        /// 解析Loop
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="intBraceCount"></param>
        /// <returns></returns>
        public static string ConvertTags_Loop(ref string strSource, Match oMatch, ref string strSpaces)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //分析loop , 如果是 </loop>
            if (oMatch.Value.Trim().ToLower() == @"</loop>")
            {
                //如果是 </loop> 则输出 } 
                strSpaces = strSpaces.RTrim("\t");
                strReturn += strSpaces + "}" + Environment.NewLine;

                //去除标签
                strSource = strSource.Replace(oMatch.Value, "");
            }
            else
            {
                //找到 <loop name="List" var="var" key="key" start="1" > 
                Match oLoopMatch = oTags_oneLoop.Match(strSource, oMatch.Index);
                if (oLoopMatch.Success)
                {

                    bool blnIsRightGrammar = false;//默认语法错误

                    //分析attribute
                    Dictionary<string, string> oDictionary = GetAttribute(oLoopMatch.Groups["attribute"].ToString());
                    if (oDictionary.ContainsKey("name") && oDictionary.ContainsKey("var"))
                    {
                        blnIsRightGrammar = true;

                        //计算起始
                        int intStatr = 1;
                        if (oDictionary.ContainsKey("start"))
                        {
                            int.TryParse(oDictionary["start"], out intStatr);
                        }

                        //key
                        string strKey = string.Empty;
                        if (oDictionary.ContainsKey("key"))
                            strKey = oDictionary["key"];

                        //int index 的名称 
                        string strIndex = StringExtension.GetGuidStrWithNumberAndChar();

                        //输出Csharp代码
                        strReturn += strSpaces + string.Format("int  int{0} = 0 + {1} -1;", strIndex, intStatr) + Environment.NewLine;
                        strReturn += strSpaces + string.Format("foreach (DataRow oTempRows{0} in getDT(\"{1}\").Rows)", strIndex, oDictionary["name"]) + Environment.NewLine;
                        strReturn += strSpaces +               "{" + Environment.NewLine;

                        strSpaces = strSpaces + "\t";
                        strReturn += strSpaces + string.Format(     "//记录并保存当前位置") + Environment.NewLine;
                        strReturn += strSpaces + string.Format(     "int{0}++;", strIndex, intStatr) + Environment.NewLine;
                        strReturn += strSpaces + string.Format(     "SaveVar(\"{0}\", int{1});", strKey, strIndex) + Environment.NewLine;

                        strReturn += strSpaces + Environment.NewLine;
                        strReturn += strSpaces + string.Format(     "//保存行变量") + Environment.NewLine;
                        strReturn += strSpaces + string.Format(     "SaveRow(\"{0}\", oTempRows{1});", oDictionary["var"], strIndex) + Environment.NewLine;

                    }

                    if (blnIsRightGrammar == false)//如果语法错误
                    {
                        strSource = strSource.Replace(oLoopMatch.Value, TagsError.GetTagsError(TagsError.TagsErrorAnalysis.ArrtibuteError));
                    }
                    else
                    {
                        strSource = strSource.Replace(oLoopMatch.Value, "");
                    }
                }
                else
                {
                    //去除标签
                    strSource = strSource.Replace(oMatch.Value, "");
                }
            }

            //去除标签语法
            if (!strSource.IsEmpty() && strSource.Length > oMatch.Index + 1)
            {
                strSource = strSource.Substring(oMatch.Index);
            }

            return strReturn;
        }
    }
}